/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.vsu.cs.piit.vokod.TableModels;

import datechooser.beans.DateChooserCombo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import ru.vsu.cs.piit.swing.*;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumnModel;
import ru.vsu.cs.piit.vokod.ObservableArrayList;
import ru.vsu.cs.piit.vokod.controller.Maps;
import ru.vsu.cs.piit.vokod.model.DailyMove;
import ru.vsu.cs.piit.vokod.model.Ward;
import ru.vsu.cs.piit.swing.ColumnGroup;

/**
 *
 * @author Галчонок
 */
public class MoveTable extends JTable {

    ListSelectionListener rsl = new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
//            removeKeyListener(kl);
//            getSelectionModel().removeListSelectionListener(rsl);
//            getModel().setAddMode(false);
            if (!getModel().saveNewRow()) {
                //выдать предупреждение +
                //getSelectionModel().setSelectionInterval(0, e.getFirstIndex());
            }
        }
    };

    KeyListener kl = new KeyListener() {

        public void keyTyped(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        public void keyPressed(KeyEvent e) {
            if ((getSelectedRow()==getRowCount()-1)&&(e.getKeyCode()==10))//Enter
            {
                getModel().saveNewRow();
                //getSelectionModel().removeListSelectionListener(rsl);                
                //getSelectionModel().setSelectionInterval(0, getRowCount()-1);
                //getSelectionModel().addListSelectionListener(rsl);
            }
           // throw new UnsupportedOperationException("Not supported yet.");
        }

        public void keyReleased(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    };

    public MoveTable(ObservableArrayList<DailyMove> data) {
        this.setModel(new MoveTableModel(data));
        
        DateChooserCombo ch = new DateChooserCombo();
        ch.setBehavior(datechooser.model.multiple.MultyModelBehavior.SELECT_SINGLE);
        ch.setCurrentNavigateIndex(0);
        ch.setNothingAllowed(false);
        ch.setFormat(2);

        this.setDefaultEditor(
            Date.class, ch.createTableCellEditor());
        //this.setDefaultRenderer(Date.class, ch.createTableCellRenderer());
        this.setDefaultEditor(
            Ward.class, new EntityCellEditor(Maps.getWardList()));
        this.setRowHeight(this.getRowHeight()+5);
        this.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.getTableHeader().setPreferredSize(new Dimension(0, 70));

        TableColumnModel cm = this.getColumnModel();
        ColumnGroup g_enter = new ColumnGroup("<html>Поступило больных<br>(без перевед. внутри дисп.)");
        g_enter.add(cm.getColumn(3));
        g_enter.add(cm.getColumn(4));
        g_enter.add(cm.getColumn(5));
        g_enter.add(cm.getColumn(6));
        ColumnGroup g_move = new ColumnGroup("Переведено больных");
        g_move.add(cm.getColumn(8));
        g_move.add(cm.getColumn(9));
        g_move.add(cm.getColumn(10));
        g_move.add(cm.getColumn(7));
        ColumnGroup g_leave = new ColumnGroup("Выписано");
        g_leave.add(cm.getColumn(12));
        g_leave.add(cm.getColumn(11));
        ColumnGroup g_evening = new ColumnGroup("<html>Состоит б-ных<br>на нач.тек.суток");
        g_evening.add(cm.getColumn(14));
        g_evening.add(cm.getColumn(15));


        GroupableTableHeader header = (GroupableTableHeader)this.getTableHeader();
        header.addColumnGroup(g_enter);
        header.addColumnGroup(g_move);
        header.addColumnGroup(g_leave);
        header.addColumnGroup(g_evening);

    }

    @Override
    public MoveTableModel getModel() {
        return (MoveTableModel)super.getModel();
    }

    @Override
    public void setModel(TableModel dataModel) {
        if (dataModel instanceof MoveTableModel)
            super.setModel(dataModel);
        else
            super.setModel(new MoveTableModel(new ObservableArrayList<DailyMove>()));
    }

    public void setAddMode(boolean AddMode)
    {
        this.getModel().setAddMode(AddMode);
        this.getSelectionModel().setSelectionInterval(0, this.getRowCount()-1);
        this.getSelectionModel().addListSelectionListener(rsl);
        this.addKeyListener(kl);
        this.requestFocus();
    }

    public void setEditMode(boolean EditMode){
        this.getModel().setEditMode(EditMode);
    }

    public void delCurrentRow()
    {
        getModel().delRow(getSelectedRow());
    }

    @Override
    protected JTableHeader createDefaultTableHeader() {
        return new GroupableTableHeader(columnModel);
    }
    
}
